<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- logo的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域
      1. :model传入表单数据
      2. :rules传入表单校验规则
       -->
      <el-form :model="form" ref="fm" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item prop="repassword">
          <el-input
            type="password"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.repassword"
          ></el-input>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-form-item>
          <el-button class="btn-all" type="primary" @click="onSubmit"
            >注册</el-button
          >
          <el-link type="info">
            <router-link to="/login">去登录</router-link>
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 导入注册的后台接口
import { regApi } from '@/api/user'
export default {
  name: 'Reg',
  data () {
    const validateSame = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      // 注册表单数据
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      // 注册表单的校验规则
      rules: {
        // 用户名的校验
        username: [
          // 非空校验
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // 长度校验
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的字母数字', trigger: 'blur' }
        ],
        // 密码的校验
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        // 确认密码的校验
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          { validator: validateSame, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async onSubmit () {
      try {
        await this.$refs.fm.validate()
        console.log('注册表单数据 :', this.form)
        const { data } = await regApi(this.form)
        console.log('注册返回的数据 :', data)
        if (data.code === 0) {
          this.$message.success(data.message)
          this.$router.push('/login')
        } else {
          this.$message.error(data.message)
        }
      } catch (error) {
        console.log('校验失败')
        console.log('error')
      }
    }
  }
}
</script>
